package main

import (
	pb "ihos/ihos"
	"log"

	tm "github.com/golang/protobuf/ptypes/timestamp"
)

type huanzhe struct {
	id        int32 //PK
	grxx      int32 //个人信息 => basicinfo PK
	jksj      int32 //健康数据 => bodyinfo PK
	dlxx      int32 //登录信息 => denglu PK
	jlsj      int64 //建立时间，秒
	yiyuan_id int32
}

func (w *huanzhe) GetFromDB(id int32) error {
	sql := "SELECT id,grxx,jksj,dlxx,jlsj,yiyuan_id FROM huanzhe WHERE id=$1"
	return DBConn.db.QueryRow(sql, id).Scan(&w.id, &w.grxx, &w.jksj, &w.dlxx, &w.jlsj, &w.yiyuan_id)
}

func (w *huanzhe) GetByGrxx(grxx_id int32) error {
	sql := "SELECT id,grxx,jksj,dlxx,jlsj,yiyuan_id FROM huanzhe WHERE grxx=$1"
	return DBConn.db.QueryRow(sql, grxx_id).Scan(&w.id, &w.grxx, &w.jksj, &w.dlxx, &w.jlsj, &w.yiyuan_id)
}

func (w *huanzhe) GetByGrxxAndDlxx(grxx_id, dlxx_id int32) error {
	sql := "SELECT id,grxx,jksj,dlxx,jlsj,yiyuan_id FROM huanzhe WHERE grxx=$1 AND dlxx=$2"
	return DBConn.db.QueryRow(sql, grxx_id, dlxx_id).Scan(&w.id, &w.grxx, &w.jksj, &w.dlxx, &w.jlsj, &w.yiyuan_id)
}

func (w *huanzhe) GetByDlxx(dlxx_id int32) error {
	sql := "SELECT id,grxx,jksj,dlxx,jlsj,yiyuan_id FROM huanzhe WHERE dlxx=$1"
	return DBConn.db.QueryRow(sql, dlxx_id).Scan(&w.id, &w.grxx, &w.jksj, &w.dlxx, &w.jlsj, &w.yiyuan_id)
}

func (w *huanzhe) SaveToDB(id int32) error {
	sql := ""
	var err error
	if id > 0 { //update
		sql = "UPDATE huanzhe set grxx=$1,jksj=$2,dlxx=$3,jlsj=$4,yiyuan_id=$5 where id=$6 RETURNING id"
		err = DBConn.db.QueryRow(sql, w.grxx, w.jksj, w.dlxx, w.jlsj, w.yiyuan_id, w.id).Scan(&w.id)
	} else { //insert
		sql = "INSERT INTO huanzhe(grxx,jksj,dlxx,jlsj,yiyuan_id) VALUES($1,$2,$3,$4,$5) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.grxx, w.jksj, w.dlxx, w.jlsj, w.yiyuan_id).Scan(&w.id)
	}
	return err
}

func (w *huanzhe) toPB() *pb.HuanZheInfoL3 {
	b := &basicinfo{}
	if err := b.GetFromDB(w.grxx); err != nil {
		log.Println("grxx:", err)
		// return nil
	}
	j := &bodyinfo{}
	if w.jksj > 0 {
		if err := j.GetFromDB(w.jksj); err != nil {
			log.Println("jksj:", err)
			// return nil
		}
	}
	d := &denglu{}
	if err := d.GetFromDB(w.dlxx); err != nil {
		log.Println("dlxx:", err)
		// return nil
	}
	js, err := new(jiuzhenren).GetByHz(w.id)
	jzr := make([]*pb.JiuZhenRenInfoL1, 0)
	if err != nil {
		log.Println("jiuzhenren:", err)
		// return nil
	} else {
		for _, v := range js {
			jzr = append(jzr, v.toPB())
		}
	}
	jzk, err := new(jiuzhenka).GetByHz(w.id)
	jzks := make([]*pb.JiuZhenKaInfoL1, 0)
	if err != nil {
		log.Println("jiuzhenka:", err)
		// return nil
	} else {
		for _, v := range jzk {
			log.Println(v.toPB())
			jzks = append(jzks, v.toPB())
		}
	}
	y := &yiyuan{id: w.yiyuan_id}
	err = y.GetFromDB(w.yiyuan_id)
	if err != nil {
		log.Println(err)
	}
	return &pb.HuanZheInfoL3{
		ID:   w.id,
		GRXX: b.toPB(),
		JTDZ: &pb.AddressInfoL1{
			DZ:    b.add_dz,
			YB:    b.add_yb,
			JD:    b.add_jd,
			WD:    b.add_wd,
			SHENG: b.add_sheng,
			SHI:   b.add_shi,
			QU:    b.add_qu,
		},
		// GZDW:
		JKSJ: j.toPB(),
		// LXFS:
		JZK: jzks,
		// SBK:
		JZR:  jzr,
		DLXX: d.toPB(),
		// DLSJ:
		JLSJ: &tm.Timestamp{Seconds: w.jlsj},
		// DQSPFJ:
		// ZT:
		YY: y.toPB(),
	}
}

func (w *huanzhe) fromPB(p *pb.HuanZheInfoL3) *huanzhe {
	//个人信息+登录信息必须唯一
	if err := w.GetByGrxxAndDlxx(p.GetGRXX().GetID(), p.GetDLXX().GetJS().GetID()); err != nil {
		w.id = 0
	}
	// w.id = p.GetID()
	w.grxx = p.GetGRXX().GetID()
	w.jksj = p.GetJKSJ().GetID()
	w.dlxx = p.GetDLXX().GetJS().GetID()
	w.jlsj = p.GetJLSJ().GetSeconds()
	w.yiyuan_id = p.GetYY().GetID()
	return w
}
